/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cronus.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 *
 * @author mark
 */
public class MAddBackup extends JFrame implements ActionListener {
    
    private JButton instellen = new JButton("Instellen");
    private JButton zoeken = new JButton("Backup zoeken");
    private JTextArea backup = new JTextArea(20,40);
    
    private JFileChooser fc = new JFileChooser("backup\\");
    
    private JMenuBar menubar = new JMenuBar();
    private JMenu help = new JMenu("Helpfunctie");
    private JMenuItem helpfunctie = new JMenuItem("Uitleg");

    
    public MAddBackup(){
        
        menubar.add(help);
        help.add(helpfunctie);
        helpfunctie.addActionListener(this);
        setJMenuBar( menubar );
        
        setLayout(new FlowLayout());
        setSize(600,400);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        setTitle("Instellen Backup");
        
        add(this.zoeken);
        add(this.instellen);
        add(this.backup);
        
        instellen.addActionListener(this);
        zoeken.addActionListener(this);
        
    }
    
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == instellen){
            
            SocketConnection socketcon2 = new SocketConnection();
            SocketConnection socketcon3 = new SocketConnection();
            
            socketcon2.send("DELETE From inloggegevens");
            socketcon3.send("DELETE From rapporten");
            
            SocketConnection socketcon = new SocketConnection();            
            String q = backup.getText();
            socketcon.send(q);
            
            JOptionPane.showMessageDialog(null,"Backup ingesteld","Geslaagd",JOptionPane.INFORMATION_MESSAGE);
            
            
            
        }
            else if (e.getSource() == zoeken){
                int r = fc.showOpenDialog(null);
                if (r == JFileChooser.APPROVE_OPTION) {
                    
                File file = fc.getSelectedFile();
                
                backup.setText("");
                
                try{
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                
                String line;
                int count = 0;
                line = br.readLine();
                String newline = "\n";
                
                    while(line != null){
                        backup.append(line + newline);
                        line = br.readLine();
                        count++;
                    }
                    
                    br.close();
                }
                catch (IOException ioe){
                    System.err.println("Caught IOException");
                }
                finally{
                }

                }
            }
                    else if (e.getSource() == helpfunctie){
                        //Maak een nieuw HelpScreen aan
                        HelpScreen usermanualscreen = new HelpScreen("svn\\Cronus Client\\backup_instellen.txt");
                        //Declareer het formaat van dit scherm
                        usermanualscreen.setSize(640, 480);
                        //Maak het scherm zichtbaar
                        usermanualscreen.setVisible(true);
                    }
        
    }
    
}
